package com.qf.lanzhou.shop.api.controller;

import com.qf.lanzhou.shop.api.dao.OrderMapper;
import com.qf.lanzhou.shop.api.service.ShowOrderService;
import com.qf.lanzhou.shop.pojo.entity.ParentsOrder;
import com.qf.lanzhou.shop.pojo.entity.SonOrder;
import com.qf.lanzhou.shop.pojo.entity.User;
import com.qf.lanzhou.shop.pojo.qo.CommentQO;
import com.qf.lanzhou.shop.pojo.vo.BaseRespVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 是柠檬甜
 * @date 2021/1/14 15:40
 */
@Controller
public class ShowOrderController extends BaseController{
    @Autowired
    private ShowOrderService showOrderService;

    @Autowired
    private OrderMapper orderMapper;

    @ApiOperation("展示订单")
    @RequestMapping(value = "/showOrder")
    @ResponseBody
    private BaseRespVO showOrder(Long status, HttpServletRequest req){
        User user = (User) req.getSession().getAttribute("user");
        Long id = user.getId();
        List<SonOrder> sonOrders = showOrderService.selectOrder(status,id);
        for (int i = 0; i < sonOrders.size(); i++) {
            //获取父订单
            ParentsOrder parentsOrder = orderMapper.selectBySonOrderId(sonOrders.get(i).getId());
            System.out.println("子订单id： "+sonOrders.get(i).getId());
            System.out.println("获取父订单： "+parentsOrder);
            if (parentsOrder!=null){
                if (parentsOrder.getUserComments()!= null && !"".equals(parentsOrder.getUserComments())){
                    sonOrders.get(i).setComments(parentsOrder.getUserComments());
                }
            }
        }
        System.out.println("dasdas"+sonOrders);
        if (sonOrders.size()==0){
            return convertSuccessResult(0);
        }
        return convertSuccessResult(sonOrders);
    }

    @ApiOperation("修改订单状态")
    @RequestMapping(value = "/changeStatus")
    @ResponseBody
    private BaseRespVO changeStatus(Long status,Long parentsOrderId){
        String str = showOrderService.changeStatus(status, parentsOrderId);
        return convertSuccessResult(str);
    }

    @PostMapping(value = "/comments")
    @ResponseBody
    @ApiOperation(value = "订单评论接口")
    public BaseRespVO comments(Long id,String comments){
        CommentQO commentQO = new CommentQO();
        commentQO.setId(id);
        commentQO.setComment(comments);
        int i = showOrderService.addComments(commentQO);
        if (i != 1){
            return convertErrorResult("系统繁忙");
        }
        return convertSuccessResult("评论成功啦");
    }
}
